//
//  ViewController.m
//  03-28-net2
//
//  Created by student on 16/3/28.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import <TBXML.h>
#import <SVProgressHUD.h>

#import "NetManager.h"
#import "def.h"
@interface ViewController ()<NetManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// GET JSON
- (IBAction)loginBtn1Pressed:(id)sender {
    //请求地址的字符串
    NSString *str=[NSString stringWithFormat:@"http://192.168.13.31:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",self.usernameTextField.text,self.pwdTextField.text];
    //请求网址
    NSURL *url=[NSURL URLWithString:str];
    
    //请求
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    
    [SVProgressHUD showWithStatus:@"正在登陆"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    
    //发送异步请求
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError==nil) {
            NSString *result=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",result);
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            //            {"success":"登录成功"}
            //{"error":"密码不正确"}
            if (dict[@"error"] !=nil) {
                NSString *message = [NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
                
                //登录失败 显示错误信息
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
                [SVProgressHUD showWithStatus:message];
            }
            else
            {
                //提示登陆成功
                [SVProgressHUD showWithStatus:@"登陆成功"];
                //1s 后跳转以下页面
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    //隐藏HUD
                    [SVProgressHUD dismiss];
                    //跳转下一条
                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
                });
            }
        }
        else
        {
            NSLog(@"%@ %@",connectionError,[connectionError localizedDescription]);
            NSString *message = [NSString stringWithFormat:@"连接错误:%@",[connectionError localizedDescription]];
            UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}
// POST JSON
- (IBAction)loginBtn2Pressed:(id)sender {
//    @"http://192.168.13.31:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON"
    
    NSString *str=@"http://192.168.13.31:8080/MJServer/login";
    NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@&method=get&type=JSON",self.usernameTextField.text,self.pwdTextField.text];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    urlRequest.HTTPMethod = @"POST";
    urlRequest.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];// =>NSString ->NSData
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil) {
            NSString *result =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",result);
            NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            
            if (dict[@"error"]!=nil) {
                //登录失败 显示错误信息
                NSString *message =[NSString stringWithFormat:@"错误信息:%@",dict[@"error"]];
                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
            else
            {
                //提示登陆成功
                [SVProgressHUD showWithStatus:@"登陆成功"];
                //1s 后跳转以下页面
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    //隐藏HUD
                    [SVProgressHUD dismiss];
                    //跳转下一条
                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
                });

                
            }
            
        }
        else{
            NSLog(@"%@ %@",connectionError,[connectionError localizedDescription]);
        }
    }];
    
}
// POST XML
- (IBAction)loginBtn3Pressed:(id)sender {
    //    @"http://192.168.13.31:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON"
    
    NSString *str=@"http://192.168.13.31:8080/MJServer/login";
    NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@&method=get&type=XML",self.usernameTextField.text,self.pwdTextField.text];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *urlRequest =[NSMutableURLRequest requestWithURL:url];
    urlRequest.HTTPMethod = @"POST";
    urlRequest.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];//NSString -> NSData
    
    //显示
    
    [SVProgressHUD showWithStatus:@"正在登陆"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError ==nil) {
            NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",result);
            //<error>密码不正确</error> // 当前就一个节点
            //<success>登录成功</success>
            
            TBXML *xml=[[TBXML alloc]initWithXMLData:data error:nil];
            
            TBXMLElement *root =xml.rootXMLElement;//得到根节点
            NSString *elementName = [TBXML elementName:root];//节点名字
            NSString *info =[TBXML textForElement:root];//节点里的内容
            NSLog(@"%@",info);
            if ([elementName isEqualToString:@"error"]) {//判断节点名字是success还是error
                
                //隐藏
                [SVProgressHUD dismiss];
                
                //登录失败,显示错误信息
                
                NSString *message = [NSString stringWithFormat:@"错误信息:%@",info];
                
                UIAlertController *alert =[UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
                
                
                UIAlertAction *action=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            else
            {
                [SVProgressHUD showInfoWithStatus:@"登陆成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    //登陆成功
                    [self performSegueWithIdentifier:@"gotoMain" sender:nil];
                });
            }
        }
        else
        {
            //连接错误
            NSLog(@"%@ %@",connectionError,[connectionError localizedDescription]);
        }
    }];
    
    
}

- (IBAction)btnX1:(id)sender {
    BOOL logined =[NetManager loginWithUserName:self.usernameTextField.text andPassword:self.pwdTextField.text];
    
    
    if(logined)
    {
        // 提示登陆成功
        [SVProgressHUD showWithStatus:@"登陆成功"];
        // 1s后跳转下以页面
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 隐藏HUD
            [SVProgressHUD dismiss];
            // 条状下一页
            [self performSegueWithIdentifier:@"gotoMain" sender:nil];
        });
    }
    else
    {
        NSString *message = @"登陆失败";
        // 登陆失败 显示错误信息
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alert addAction:aciton];
        [self presentViewController:alert animated:YES completion:nil];
    }

}
- (IBAction)btnX2:(id)sender {
    [SVProgressHUD showWithStatus:@"正在登陆"];
    [NetManager loginWithUserName:self.usernameTextField.text
                      andPassword:self.pwdTextField.text
                     successBlock:^{
                         // 提示登陆成功
                         [SVProgressHUD showWithStatus:@"登陆成功"];
                         // 1s后跳转下以页面
                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                             // 隐藏HUD
                             [SVProgressHUD dismiss];
                             // 条状下一页
                             [self performSegueWithIdentifier:@"gotoMain" sender:nil];
                         });
                     } failedBlock:^(NSError *error){
                         [SVProgressHUD dismiss];
                         NSString *message = @"登陆失败";
                         // 登陆失败 显示错误信息
                         UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
                         UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                             [self dismissViewControllerAnimated:YES completion:nil];
                         }];
                         [alert addAction:aciton];
                         [self presentViewController:alert animated:YES completion:nil];
                     }];
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//代理模式
- (IBAction)loginBtn1:(id)sender
{
     [SVProgressHUD showWithStatus:@"正在登陆"];
    
    NetManager *net = [NetManager shareNetManager];
    
    net.delegate=self;
    [net loginWithUserName:self.usernameTextField.text andPassword:self.pwdTextField.text];
}

-(void)loginsuccess
{
    // 提示登陆成功
    [SVProgressHUD showWithStatus:@"登陆成功"];
    // 1s后跳转下以页面
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 隐藏HUD
        [SVProgressHUD dismiss];
        // 下一页
        [self performSegueWithIdentifier:@"gotoMain" sender:nil];
    });

}
-(void)loginfailed:(NSError *)error
{
    [SVProgressHUD dismiss];
    NSString *message = [NSString stringWithFormat:@"错误信息:%@",[error localizedDescription]];
    // 登陆失败 显示错误信息
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:aciton];
    [self presentViewController:alert animated:YES completion:nil];
}



//观察者模式
-(void)logsuccess
{
    // 提示登陆成功
    [SVProgressHUD showWithStatus:@"登陆成功"];
    // 1s后跳转下以页面
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 隐藏HUD
        [SVProgressHUD dismiss];
        // 下一页
        [self performSegueWithIdentifier:@"gotoMain" sender:nil];
    });
    
}
-(void)logfailed:(NSNotification *)n
{
    NSError *error=n.userInfo[@"error"];
    [SVProgressHUD dismiss];
    NSString *message = [NSString stringWithFormat:@"错误信息:%@",[error localizedDescription]];
    // 登陆失败 显示错误信息
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *aciton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:aciton];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //添加相关网络请求结果通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logsuccess) name:kNotificationLoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logfailed:) name:kNotificationLoginFail object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (IBAction)loginBtn2:(id)sender
{
    [SVProgressHUD showWithStatus:@"正在登陆"];
    NetManager *net=[NetManager shareNetManager];
    
    [net loginWithUserName:self.usernameTextField.text andPassword:self.pwdTextField.text];
}


@end
